package com.lvyq.javaprint.controller;

import com.lvyq.javaprint.model.BarCodeData;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;

/**
 * @author lvyq 条码打印示例-北洋
 * @version 1.0
 * Create by 2024-12-17 15:48
 */
@RestController
@RequestMapping("/print")
@CrossOrigin
public class BarCodePrintController {

    @RequestMapping("/barCodePrint")
    public  void barCodePrint(BarCodeData data) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {

        try{
            // File jarFile = new File("D:\\javaPrint11Jar\\lib\\LabelPrinterJavaSDK.jar");
            //将第三方jar包目录与本项目的jar包位置保持同级即可
            String SDK_PATH =  System.getProperty("user.dir")+"/libs/LabelPrinterJavaSDK.jar";
            SDK_PATH =SDK_PATH.replace("/","\\");
            File jarFile = new File(SDK_PATH);
            // 创建URLClassLoader
            URL[] urls = { jarFile.toURI().toURL() };
            URLClassLoader classLoader = new URLClassLoader(urls);

            // 加载类
            Class<?>   printerClass = classLoader.loadClass("com.snbc.sdk.LabelPrinter");
            // 创建类的实例（如果类有默认构造函数）
            Object instance = printerClass.getDeclaredConstructor().newInstance();

            // 调用方法（为"LoadLibrary",没有返回类型，一个入参）
            Method LoadLibrary = printerClass.getMethod("LoadLibrary",String.class);
            Object[] LoadLibraryVal = {"LabelPrinterSDK"};
            LoadLibrary.invoke(instance,LoadLibraryVal);


            //建立打印机连接（DiscoverPrinter ，int 入参[3表示BPLA]  ，返回类型为String[]）
            Method DiscoverPrinter = printerClass.getMethod("DiscoverPrinter",int.class);
            Object[] DiscoverPrinterVal = {3};
            String[] printerList =(String[]) DiscoverPrinter.invoke(instance,DiscoverPrinterVal);
            String portInfo = printerList[0];
            //6.BPLA 连接
            Method ConnectPrinter = printerClass.getMethod("ConnectPrinter",int.class, String.class, int.class);
            Object[] ConnectPrinterVal = {3, portInfo, 6};
            int r=(int) ConnectPrinter.invoke(instance,ConnectPrinterVal);
            if (r != 0) {
                System.out.println("打印机连接异常 " + r);
            }

            //设置标签的宽度以及高度
            // printer.SetLabelSize(450, 250);
            Method SetLabelSize = printerClass.getMethod("SetLabelSize",int.class,int.class);
            Object[] SetLabelSizeVal = {450,250};
            SetLabelSize.invoke(instance,SetLabelSizeVal);
            //打印模式
            // 1-撕离模式；2-剥离模式；3-回卷模式；4
            //出纸口与打印头间的距离。仅BPLA回卷模式支持。
            //printer.SetPrintMode(3,250);
            Method SetPrintMode = printerClass.getMethod("SetPrintMode",int.class,int.class);
            Object[] SetPrintModeVal = {3,250};
            SetPrintMode.invoke(instance,SetPrintModeVal);
            try{
                //宽高20 加粗 宋体
                //  printer.PrintTrueTypeText(0, 200, "P18", 15, 19, data.getCheckName(), 0, 2);
                Method PrintTrueTypeText1 = printerClass.getMethod("PrintTrueTypeText",int.class,int.class,String.class,int.class,int.class,String.class,int.class,int.class);
                Object[] PrintTrueTypeTextVal1 = {20, 200, "P18", 15, 19, data.getCheckName(), 0, 2};
                PrintTrueTypeText1.invoke(instance,PrintTrueTypeTextVal1);

                //HRB400E  φ 初检
                //printer.PrintTrueTypeText(0, 170, "P18", 15, 19, data.getSpec(), 0, 2);

                Method PrintTrueTypeText2 = printerClass.getMethod("PrintTrueTypeText",int.class,int.class,String.class,int.class,int.class,String.class,int.class,int.class);
                Object[] PrintTrueTypeTextVal2 = {20, 170, "P18", 15, 19, data.getSpec(), 0, 2};
                PrintTrueTypeText2.invoke(instance,PrintTrueTypeTextVal2);

                //打印条形码 横坐标 纵坐标 条码类型（1.CODE128）  旋转角度   条码内容    条码打印高度  条码注释字符打印模式（1下方） 窄条宽度 宽条宽度
                //printer.PrintBarcode1D(40, 40, 1, 0, data.getCode(), 80, 1, 2, 6);
                Method PrintBarcode1D = printerClass.getMethod("PrintBarcode1D",
                        int.class,int.class,int.class,int.class,String.class, int.class,int.class,int.class,int.class);
                Object[] PrintBarcode1DVal = {40, 40, 1, 0, data.getCode(), 80, 1, 2, 6};
                PrintBarcode1D.invoke(instance,PrintBarcode1DVal);

                // printer.PrintTrueTypeText(0, 20, "P18", 15, 19, "状态:待检√在检_已检_备份_", 0, 2);
                Method PrintTrueTypeText3 = printerClass.getMethod("PrintTrueTypeText",int.class,int.class,String.class,int.class,int.class,String.class,int.class,int.class);
                Object[] PrintTrueTypeTextVal3 = {20, 20, "P18", 15, 19, "状态:待检√在检_已检_备份_", 0, 2};
                PrintTrueTypeText3.invoke(instance,PrintTrueTypeTextVal3);

                //开启打印
                //printer.PrintLabel(1, 1);
                Method PrintLabel = printerClass.getMethod("PrintLabel",int.class,int.class);
                Object[] PrintLabelVal = {1, 1};
                PrintLabel.invoke(instance,PrintLabelVal);

        }catch (Exception e){
            e.printStackTrace();
        }
        }catch (Exception exception){
            System.out.println("条码打印异常");
        }

    }


}
